#! /bin/sh
# autogen
# 
# --
# Copyright (c) 2012, Christophe Huriaux
# Distributed under the terms of the MIT license.
# http://www.opensource.org/licenses/mit-license.php
# -- 

progname=`basename $0`
top_srcdir=`dirname $0`

verbose="";
quiet="false"
mode="usage"

usage()
{
  echo
  echo "Usage: ${progname} [-c|-p|-h] [-q] [-v]"
  echo
  echo "Operation modes:"
  echo "	-c, --clean     clean, remove all aclocal/autoconf/automake generated files"
  echo "	-h, --help      display this message and exit"
  echo "	-r, --reconf    regenerate Makefile.in"
  echo "	-q, --quiet     don't display directories"
  echo "	-v, --verbose   pass -v to autotools"
  echo
  exit 1;
}

while test $# -gt 0; 
do
    case $1 in
        -h|--help)
            usage ;;
        -q|--quiet)
            quiet="true";
            shift;;
        -v|--verbose)
            verbose="-v";
            shift;;
        -c|--clean)
            mode="clean";
            shift;;
        -r|--reconf)
            mode="autoreconf";
            shift;;
        -b*|--build-protocol-*)
            optProtocol=`expr "x$1" : 'x-b\(.*\)'`
            
            if test "x$optProtocol" = "x";
            then
                optProtocol=`expr "x$1" : 'x--build-protocol-\(.*\)'`
                if test "x$optProtocol" = "x";
                then
                    echo "invalid protocol specified in option $1" ;
                    exit 1;
                fi
            fi
           
            protocolNum=`expr "x$optProtocol" : 'x\([0-9]*\)$'`
            if test "x$protocolNum" = "x" -a "x$optProtocol" != "xdefault";
            then                
                echo "invalid protocol specified in option $1" ;
                exit 1;
            fi
            
            mode="buildprotocol";
            shift;;
        -*) 
            echo "unknown option $1" ;
            usage ;;
        *) 
            echo "invalid parameter $1" ;
            usage ;;
    esac
done

case $mode in
    usage)
        usage
    ;;
    
    autoreconf)
        AUTORECONF=${AUTORECONF-autoreconf}
        if test -z "$AUTORECONF"; then
            echo "You must have autoreconf installed to run $program"
            exit 1
        fi

        confs=`find . -name 'configure.ac' -print`
        for i in $confs; do
            dir=`dirname $i`;
            configure=`basename $i`;
            ( 
                test "$quiet" = "true" || echo "$dir";
                cd $dir;
                ${AUTORECONF} -i --no-recursive -I$top_srcdir/aclocal/ $verbose;
            )
        done
    ;;
    
    buildprotocol)
        protocolsDir="$top_srcdir/resources/build_packet"
        
        if test ! -d $protocolsDir;
        then
            echo "cannot access protocol directory $protocolsDir";
            exit 1;
        fi
        
        echo "scanning $protocolsDir ...";
        protocolsFiles=`find $protocolsDir -name "mcprotocol-*.xml"`
        
        if test "x$protocolsFiles" = "x";
        then
            echo "no protocol definition files found.";
            exit 1;
        fi
        
        protocolsList=`ls -1v $protocolsFiles | grep "[[:digit:]]*" -o | sed -e :a -e N -e 's/\n/,/' -e ta`
        lastProtocol=`ls -1v $protocolsFiles | grep "[[:digit:]]*" -o | tail -1`
        
        if test "x$optProtocol" = "xdefault";
        then
            protocolNum=$lastProtocol;
        fi
        
        echo "I found definitions files for the following protocols version: $protocolsList"
        
        if test ! -f "$protocolsDir/mcprotocol-$protocolNum.xml";
        then
            echo "requested protocol version $protocolNum not found."
            exit 1
        fi
        
        echo "generating sources for protocol v$protocolNum ..."
        
        cd $top_srcdir/resources/build_packet
        ./build_packet.pl -i mcprotocol-$protocolNum.xml -s ../../src/network/packet/ -h ../../include/network/packet/
    ;;

    clean)
        test "$quiet" = "true" || echo "removing automake generated Makefile.in files"
        files=`find . -name 'Makefile.am' -print | sed -e 's%\.am%\.in%g'` ;
        for i in $files; 
        do 
            if test -f $i; 
            then
                rm -f $i
                test "$verbose" = "-v" && echo "$i"    
            fi; 
        done

        test "$quiet" = "true" || echo "removing configure files"
        files=`find . -name 'configure' -print` ;
        test "$verbose" = "-v" && test -n "$files" && echo "$files" ;
        for i in $files; 
        do 
            if test -f $i; 
            then
                rm -f $i config.sub config.guess depcomp install-sh mdate-sh missing \
                mkinstalldirs texinfo.tex compile
                test "$verbose" = "-v" && echo "$i"    
            fi; 
        done

        test "$quiet" = "true" || echo "removing aclocal.m4 files"
        files=`find . -name 'aclocal.m4' -print` ;
        test "$verbose" = "-v" && test -n "$files" && echo "$files" ;
        for i in $files; 
        do 
            if test -f $i; 
            then
                rm -f $i
                test "$verbose" = "-v" && echo "$i"    
            fi; 
        done
        
        test "$quiet" = "true" || echo "cleaning up previous protocol source files"
        test "$verbose" = "-v" && echo "pfs source files"
        rm -f $top_srcdir/src/network/packet/pfs*.cpp
        test "$verbose" = "-v" && echo "pts source files"
        rm -f $top_srcdir/src/network/packet/pts*.cpp
        test "$verbose" = "-v" && echo "packets source file"
        rm -f $top_srcdir/src/network/packet/packets.cpp
        test "$verbose" = "-v" && echo "pfs header files"
        rm -f $top_srcdir/include/network/packet/pfs*.h
        test "$verbose" = "-v" && echo "pts header files"
        rm -f $top_srcdir/include/network/packet/pts*.h
        test "$verbose" = "-v" && echo "packets header file"
        rm -f $top_srcdir/include/network/packet/packets.h

        find . -name '*~' -print | xargs rm -f
        find . -name 'config.status' -print | xargs rm -f
        find . -name 'config.log' -print | xargs rm -f
        find . -name 'config.cache' -print | xargs rm -f
        find . -name 'Makefile' -print | xargs rm -f
        find . -name '.deps' -print | xargs rm -rf
        find . -name '.libs' -print | xargs rm -rf
        find . -name 'stamp-h.in' | xargs rm -rf
        find . -name 'autom4te*.cache' | xargs rm -rf
        find . -name 'build-aux' | xargs rm -rf
    ;;
esac

exit 0
